package org.catlabs.patterns.jobserver.observer;

import java.util.Observable;
import java.util.Observer;

import org.catlabs.patterns.jobserver.subject.WeatherData;


public class CurrentConditionsDisplay implements Display, Observer {

	Observable observable;
	float temperature, humidity; // Only these are relevant to this display

	/**
	 * It not only holds a reference to the Subject but it also subscribes
	 * itself to the Subject (makes sense, people subscribe to magazines,
	 * magazines doesn't pick people)
	 * 
	 * @param weatherData
	 */
	public CurrentConditionsDisplay(Observable weatherData) {
		this.observable = weatherData;
		observable.addObserver(this); // registers itself
	}

	@Override
	public void display() {
		System.out.println("Current conditions");
		System.out.println("Temperature: " + temperature + " Celcius");
		System.out.println("Humidity: " + humidity + "%\n");
	}

	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof WeatherData) {
			WeatherData weatherData = (WeatherData) o;
			this.temperature = weatherData.getTemperature();
			this.humidity = weatherData.getHumidity();
			display();
		}
	}

}
